//
//  OtherFansVC.swift
//  LeGou
//
//  Created by lxm on 2022/7/30.
//

import UIKit

class OtherFansVC: ZMViewController {

    //MARK:--------------------属性--------------------
    var id = 0
    
    let myTeamInfo = ZMUtil.fastNilBehavior(type: MineModels.TeamAcModel.self)

    //MARK:--------------------UI控件--------------------
    @IBOutlet weak var stack: UIStackView!
    
    let loading = XMLoadingView()
        
    @IBOutlet weak var fans: UILabel!
    
    @IBOutlet weak var direct: UILabel!
    
    @IBOutlet weak var treeView: MyTeamUserTreeView!



    //MARK:--------------------生命周期--------------------
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        addConstraints()
        bindAction()
        loadMyTeamInfo()
    }
    //MARK:--------------------UI--------------------
    func setUI() {
        title = "TA的粉丝"
    }
    func addConstraints() {
        
    }
    
    //MARK:--------------------methods--------------------
    func bindAction() {
        myTeamInfo.subscribe(onNext:{ [unowned self] resp in
            guard let resp = resp else {return}
            self.fans.text = "\(resp.currentUser.fansNum)"
            self.direct.text = "\(resp.currentUser.inviteNum)"
            self.treeView.me.zm.setImage(urlStr: resp.currentUser.avatar, placeholder: "def_user_img")
            self.treeView.vip.isHidden = resp.currentUser.serviceProviderLevel == 0

            for i in 0 ..< resp.nodes.count {
                let node = resp.nodes[i]
                let userView = self.treeView.subarray[i]
                if let u = node.user {
                    userView.avatar.zm.setImage(urlStr: u.avatar, placeholder: "def_user_img")
                    userView.name.text = u.nickname
                    userView.vip.isHidden = u.serviceProviderLevel == 0
                } else {
                    userView.name.text = "暂无"
                    userView.vip.isHidden = true
                }
            }
            
            let users = resp.nodes.filter({$0.user != nil}).map({$0.user!})
            
            for u in users {
                let v = MyTeamUserView()
                v.name.text = u.nickname
                v.avatar.zm.setImage(urlStr: u.avatar, placeholder: "def_user_img")
                v.phone.text = u.phone
                v.monthOrder.text = "\(u.monthOrder)"
                v.fans.text = "\(u.fansNum)"
                v.invite.text = "\(u.inviteNum)"
                v.totalProfit.text = u.totalProfit.zm.toString(keep: 4)
                v.todayProfit.text = u.todayProfit.zm.toString(keep: 4)
                v.monthProfit.text = "\(u.moneyProfit.zm.toString(keep: 4))"
                v.vipIcon.isHidden = u.serviceProviderLevel == 0
                stack.addArrangedSubview(v)
                v.layer.cornerRadius = 12
                v.teamBtn.isHidden = true
                v.masksToBounds = true
                
                v.wechat.isHidden = u.wechatNo.count == 0
                
                v.onWxClick = { [weak self] in
                    let v = ZMUtil.loadNibViewFullScreen(nibName: ServiceWx.self)
                    v.wxLabel.text = u.wechatNo
                    v.title.text = "微信号"
                    v.tijkfwx.isHidden = true
                    APPWindow?.addSubview(v)
                }
            }
        }).disposed(by: disposeBag)
    }
    
    func loadMyTeamInfo() {
        loading.showInView(fatherView: view)
        MineAPI.teamInfo(["userId" : id]).rxRequest(type: MineModels.TeamAcModel.self).subscribe(onNext:{ [unowned self] resp in
            self.loading.hideView(anmation: true)
            self.myTeamInfo.accept(resp)
        }, onError: { e in
            self.loading.showError(e.localizedDescription)
        }).disposed(by: disposeBag)
    }

    //MARK:--------------------delegate--------------------

}
